package baidu;

import org.omg.PortableInterceptor.INACTIVE;

import java.util.HashSet;
import java.util.Scanner;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-9-7下午 07:46
 */
public class Test2 {
    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T-- > 0) {
            long n = scanner.nextLong();
            for (long i = n; i >= 1; i--) {
                if (helper(i)) {
                    System.out.println(i);
                    break;
                }
            }

        }
    }

    private static boolean helper(long i) {
        final String s = String.valueOf(i);
        if (s.contains("0") || s.contains("4") || s.contains("5") || s.contains("6") || s.contains("7") ||
                s.contains("8") || s.contains("9")) {
            return false;
        }
        return true;
    }
}
